# coding: utf-8
try:
    import time, requests, re, hashlib, datetime, sys, os, random
    import base64
    import json
    import string
    from urllib.parse import quote
    from Crypto.Cipher import AES
    from Crypto.Cipher import PKCS1_v1_5 as Cipher_pksc1_v1_5
    from Crypto.PublicKey import RSA
except Exception as e:
    t = re.findall("d '(.*?)'", str(e))[0]
    print(f'{t}依赖未安装')
    sys.exit()


appid = 'wx7ad38ac5f24ce689'
headers = {'User-Agent': 'Mozilla/5.0 (Linux; Android 9; PBBT00 Build/PPR1.180610.011; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/70.0.3538.110 Mobile Safari/537.36 MMWEBID/3625 MicroMessenger/7.0.17.1720(0x27001134) Process/tools WeChat/arm32 NetType/4G Language/zh_CN ABI/arm64', }
host = 'http://39.108.184.87'


#----------------------------------------------------------------------------------------------------------------------
# 随机字母数字
def generator(str_size):
    return ''.join(random.choice(string.ascii_lowercase) for x in range(str_size))
# rsa加密
def public_key_encrypt(text_1):
    rsakey = RSA.importKey(public_key)
    cipher = Cipher_pksc1_v1_5.new(rsakey)
    cipher_text = base64.b64encode(cipher.encrypt(text_1.encode()))
    return cipher_text.decode()
# aes加密
BLOCK_SIZE = 16

def pad(s): return s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * \
                   chr(BLOCK_SIZE - len(s) % BLOCK_SIZE)

def unpad(s): return s[:-ord(s[len(s) - 1:])]

def AES_Encrypt(key, data):
    da = pad(data)
    cipher = AES.new(key.encode('utf8'), AES.MODE_CBC, iv.encode('utf8'))
    encryptedbytes = cipher.encrypt(da.encode('utf8'))
    encodestrs = base64.b64encode(encryptedbytes)
    enctext = encodestrs.decode('utf8')
    return enctext
def AES_Decrypt(key, text):
    data = text.encode('utf8')
    encodebytes = base64.decodebytes(data)
    cipher = AES.new(key.encode('utf8'), AES.MODE_CBC, iv.encode('utf8'))
    text_decrypted = cipher.decrypt(encodebytes)
    text_decrypted = unpad(text_decrypted)
    text_decrypted = text_decrypted.decode('utf8')
    return text_decrypted

iv = '9LEDKHWVF2YIT5K4'
public_key = '-----BEGIN PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCXLYCjqsDNKwyb4XuaYSO7VB1PzSVMtPQdRePyG266BTtv3pixLoGXJVcrGtLKdcvQUMmgpHrJTeKWlYSMOa1tWH732VMMb13pS8xm+VPeUq84JSg/g9ZaSSY6/DWGmQWDdGZS4ipcpbNWT99MRSCZa9noJxDkPJWE6AQV0+HzSwIDAQAB\n-----END PUBLIC KEY-----'


#----------------------------------------------------------------------------------------------------------------------

def timi_new ():#line:2
    return str (int (time .time ()*1000 ))#line:3
def post_qrcode ():#line:4
    try :#line:5
        O000000000O0O000O =requests .post (f'http://open.weixin.qq.com/connect/app/qrconnect?appid={appid}&bundleid=(null)&scope=snsapi_userinfo&state=w&from=message&isappinstalled=0',headers =headers ).text #line:8
        O000OO0000O0OO0OO =re .findall ('auth_qrcode" src="(.*?)" />',O000000000O0O000O )[0 ]#line:9
        OO00000OOOO000O0O =O000OO0000O0OO0OO .split ('qrcode/')[1 ]#line:10
        return OO00000OOOO000O0O #line:11
    except Exception as OOOO0OO0OO0O0O0OO :#line:12
        print (OOOO0OO0OO0O0O0OO )#line:13
def gettasklist (O0O00O0OOOOO00O0O ,O0OO0O0O00OOO0OO0 ,O00OOOO0O0O0O0O00 ):#line:14
    try :#line:15
        OO000O000000000OO =requests .request ('get',f'{host}/api/gettasklist.asp?username={O0O00O0OOOOO00O0O}&password={O0OO0O0O00OOO0OO0}&page=1&y=1&appid={appid}&k={O00OOOO0O0O0O0O00}').json ()#line:17
        if OO000O000000000OO ['ret']=='0':#line:19
            print (f"账号信息->任务状态:{OO000O000000000OO['date'][0]['State']}")#line:20
            if OO000O000000000OO ['date'][0 ]['State']=='二维码已失效'or OO000O000000000OO ['date'][0 ]['State']=='管理员终止':#line:21
                return True #line:22
        return False #line:23
    except Exception as O0OOOO0OO0OOO000O :#line:24
        print (O0OOOO0OO0OOO000O )#line:25
def post_code (O0000O00OO0O0000O ,O0O0O00OOOO00O0O0 ,OO0O0OO0O0O0000O0 ,O0000O0O00O000O0O ):#line:26
    O00OOOOO0OOOO00OO =int (time .time ())#line:27
    try :#line:28
        while True :#line:29
            if gettasklist (O0O0O00OOOO00O0O0 ,OO0O0OO0O0O0000O0 ,O0000O0O00O000O0O ):#line:30
                return False #line:31
            O0O0O0O0OOOO0OO0O =requests .post (f'http://long.open.weixin.qq.com/connect/l/qrconnect?uuid={O0000O00OO0O0000O}&_={O00OOOOO0OOOO00OO})',headers =headers ).text #line:32
            if '405'in O0O0O0O0OOOO0OO0O :#line:33
                O0O00000O0O000OOO =re .findall ("wx_code='(.*?)';",O0O0O0O0OOOO0OO0O )[0 ]#line:34
                return O0O00000O0O000OOO #line:35
            time .sleep (3 )#line:36
    except Exception as O0OOO0OOOO0O0O00O :#line:37
        print (O0OOO0OOOO0O0O00O )#line:38
def post_dl (OO00OOOO0OO0000OO ,OO0OO0O0O00O0O0O0 ):#line:39
    try :#line:40
        O0OO0O0O0OOOO0O00 =[]#line:41
        OO000O0O0OO0OO000 =generator (16 )#line:42
        O0OO0OO0O0O00OOO0 =str (int (time .time ()))#line:43
        O00OO000OOOOOOOOO ={'sKey':public_key_encrypt (OO000O0O0OO0OO000 ),'user-agent':'Mozilla/5.0 (iPhone; CPU iPhone OS 14_4_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 Html5Plus/1.0 (Immersed/44) uni-app','Content-Type':'application/x-www-form-urlencoded','Host':'game.yunyoushijie.net'}#line:47
        O000O00O00OOO0OO0 ='service_time='+O0OO0OO0O0O00OOO0 #line:48
        OOOOOO000O0O0000O =quote (AES_Encrypt (key =OO000O0O0OO0OO000 ,data =O000O00O00OOO0OO0 ),'utf-8')#line:49
        OOO000000OO0O00OO =str ('service_time='+O0OO0OO0O0O00OOO0 +'&l_sign='+OOOOOO000O0O0000O +f'&code={OO00OOOO0OO0000OO}'+f'&uuid={OO0OO0O0O00O0O0O0}')#line:50
        O00O00O0000OOO000 =requests .post ('https://game.yunyoushijie.net/auth/wxLogin',headers =O00OO000OOOOOOOOO ,data =OOO000000OO0O00OO ).json ()#line:51
        if 'code'in O00O00O0000OOO000 :#line:52
            if O00O00O0000OOO000 ['code']==10000 :#line:53
                OO0O0OOO0000O00OO =O00O00O0000OOO000 ['result']['token_key']#line:54
                O0OOOOO0OOO00O00O =O00O00O0000OOO000 ['result']['token_value']#line:55
                O0OO0O0O0OOOO0O00 .append (OO0O0OOO0000O00OO )#line:56
                O0OO0O0O0OOOO0O00 .append (O0OOOOO0OOO00O00O )#line:57
                return O0OO0O0O0OOOO0O00 #line:58
            else :#line:59
                return [None ,None ]#line:60
    except Exception as OO0OOO00O0O0O0O00 :#line:61
        print (OO0OOO00O0O0O0O00 )
def addtask(elephant_user, elephant_pswd, elephant_Task_ID):
    uuid = post_qrcode()
    body = f'zcgqewm=https://open.weixin.qq.com/connect/confirm?uuid={uuid}'
    hea = {'Charset': 'UTF-8', 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
           'Cache-Control': 'no-cache', 'accept': 'application/json,text/html', 'Content-Length': '86',
           'User-Agent': 'Dalvik/2.1.0(Linux;U;Android12;2201122CBuild/SKQ1.211006.001)',
           'Host': '39.108.184.87', 'Connection': 'Keep-Alive', 'Accept-Encoding': 'gzip', }
    try:
        resp = requests.post(f'{host}/api/addtask.asp?username={elephant_user}&password={elephant_pswd}&zcsqrwid={elephant_Task_ID}',headers=hea, data=body).json()
        if 'msg' in resp:
            print(f'账号信息->提交任务:{resp["msg"]}')
        code = post_code(uuid, elephant_user, elephant_pswd, elephant_Task_ID)
        if code:
            return post_dl(OO00OOOO0OO0000OO=code, OO0OO0O0O00O0O0O0=uuid)
    except Exception as e:
        print(e)

